package com.dq.domain.address;

import com.dq.vo.baseVo.VO;
import com.dq.domain.base.BaseEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.ManyToOne;
import java.util.List;

/**
 * 地区基础表
 *
 * @author gewenzhen
 */
@Data
@Entity
@ApiModel("地区实体")
public class SysRegion extends BaseEntity implements VO.ToVO {
    //父节点
    @ManyToOne
    @ApiModelProperty(value = "父节点",hidden = true)
    protected SysRegion parent;
    //区域名称
    protected String name;
    //类型 0国家 1省份 2地市 3区县
    @Enumerated(EnumType.ORDINAL)
    protected Type type;

    public enum Type {
        country("国家"), province("省份"), city("地市"), district("区县"), town("乡镇");

        Type(String name) {
            this.name = name;
        }

        public String name;
    }

    @Data
    @ApiModel("VO")
    public static class Vo implements VO{
        @ApiModelProperty("id")
        public Long id;
        @ApiModelProperty("地区名称")
        public String name;
        @ApiModelProperty("地区子集合")
        public List<SysRegion.Vo> sysRegionList;
        @ApiModelProperty("父id")
        public Long parentId;
        @ApiModelProperty("父地区名称")
        public String parentName;
    }

    @Override
    public VO toVO(boolean isRecursion) {
        Vo vo = new Vo();
        vo.id = this.getId();
        vo.name = this.getName();
        vo.parentId = this.parent==null?null:this.getParent().getId();
        vo.parentName= this.parent==null?null:this.getParent().getName();
        return vo;
    }

    public SysRegion addChild(String childName,SysRegion parent){
        SysRegion sysRegion = new SysRegion();
        sysRegion.setName(childName);
        sysRegion.setParent(parent);
        switch (parent.getType()){
            case country:
                sysRegion.setType(Type.province);
                break;
            case province:
                sysRegion.setType(Type.city);
                break;
            case city:
                sysRegion.setType(Type.district);
                break;
            case district:
                sysRegion.setType(Type.town);
                break;
            case town:
                return null;
        }
        return sysRegion;
    }

}
